const Router = require('koa-router')
const { CategoryController } = require('../../controllers')
const { HttpDataResolve, Validator } = require('../../common')
const { Auth, SP_ADMIN, ADMIN } = require('../../middleware/auth')
const res = new HttpDataResolve()

const router = new Router({
  prefix: '/api/v1/category'
})

router.post('/', new Auth(ADMIN).m, async (ctx) => {
  const data = new Validator(ctx, {
    name: 'required|min:2|max:10',
    type: 'required|include:label,category'
  })

  const id = await CategoryController.create(data);

  ctx.response.status = 200;
  ctx.body = res.json({id});
})

router.get('/list', async ctx => {
  const data = new Validator(ctx.query, {
    keyword: '',
    type: 'include:category,label',
    page: 'number',
    size: 'number',
    order: 'include:DESC,ASC'
  }, true)
  const result = await CategoryController.getList(data);

  ctx.response.status = 200;
  ctx.body = res.json(result);
})

router.delete('/:id', new Auth(ADMIN).m, async ctx => {
  const data = new Validator(ctx.params, {
    id: "required|number"
  }, true);

  await CategoryController.delete(data);

  ctx.response.status = 200;
  ctx.body = res.success();
})

module.exports = router
